from rest_framework import serializers

from payment.models import OrderInfo, OrderGoods
from goods.models import SKU


class SKUSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = ("name","default_image")


class OrderGoodsSerializer(serializers.ModelSerializer):
    sku = SKUSerializer()

    class Meta:
        model =OrderGoods
        fields = ("count","price","sku")


class OrderSerializer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format("%Y-%m-%d %H:%M"),read_only=True)
    user = serializers.StringRelatedField(read_only=True)
    skus = OrderGoodsSerializer(many=True,read_only=True)

    class Meta:
        model = OrderInfo
        exclude = ["update_time"]
        extra_kwargs = {
            "total_count":{"read_only":True},
            "freight":{"read_only":True},
            "pay_method":{"read_only":True},
        }




# {
#         "counts": 39,
#         "list": [
#             {
#                 "order_id": "20181126102807000000004",
#                 "create_time": "2018-11-26T18:28:07.470959+08:00"
#             },
#             {
#                 "order_id": "20181126103035000000004",
#                 "create_time": "2018-11-26T18:30:35.854982+08:00"
#             },
#             ......
#         ],
#         "page": 1,
#         "pages": 8,
#         "pagesize": 5
#     }

 # {
 #        "order_id": "20181126102807000000004",
 #        "user": "zxc000",
 #        "total_count": 5,
 #        "total_amount": "52061.00",
 #        "freight": "10.00",
 #        "pay_method": 2,
 #        "status": 1,
 #        "create_time": "2018-11-26T18:28:07.470959+08:00",
 #        "skus": [
 #            {
 #                "count": 1,
 #                "price": "6499.00",
 #                "sku": {
 #                    "name": "Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机",
 #                    "default_image_url": "http://image.meiduo.site:8888/group1/M00/00/02/CtM3BVrRZCqAUxp9AAFti6upbx41220032"
 #                }
 #            },
 #            ......
 #        ]
 #    }